The following example demonstrates how to change the statistical context of a TextBlock that displays the results of a statistical function when the current item is changed. The statistical context of the TextBlock will be changed in the grid's PropertyChanged event handler by using the GetParentGroupFromItem method to retrieve the current group and set it as the new statistical context.

To simplify the code below, the DataContext of the StackPanel could have been modified rather than the DataContext of each TextBlock.

The implementation of the PropertyChanged event handler is located below.

XAML
Copy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
  <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key="cvs_orderdetails"
                                    Source="{Binding Source={x:Static Application.Current},
                                                     Path=OrderDetails}">
      <xcdg:DataGridCollectionViewSource.StatFunctions>
        <xcdg:CountFunction ResultPropertyName="orderid_count"
                            SourcePropertyName="OrderID"/>
        <xcdg:AverageFunction ResultPropertyName="unitprice_average"
                              SourcePropertyName="UnitPrice"/>             
      </xcdg:DataGridCollectionViewSource.StatFunctions>
      <xcdg:DataGridCollectionViewSource.GroupDescriptions>
        <xcdg:DataGridGroupDescription PropertyName="ProductID"/>
      </xcdg:DataGridCollectionViewSource.GroupDescriptions>
    </xcdg:DataGridCollectionViewSource>
    <xcdg:StatResultConverter x:Key="valueConverter"/>
  </Grid.Resources>
  <DockPanel>
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
      <TextBlock Text="Results for product "/>
      <TextBlock x:Name="CurrentGroupTitle" Text="{Binding Name}"/>
      <TextBlock Text=": "/>
      <TextBlock Text="     Total Orders-"/>
      <TextBlock x:Name="TotalOrders" Text="{Binding orderid_count}"/>
      <TextBlock Text="     Average Unit Price-"/>
      <TextBlock x:Name="AveragePrice"
                 Text="{Binding unitprice_average,
                        Converter={StaticResource valueConverter},
                        ConverterParameter=f2}"/>
    </StackPanel>
    <xcdg:DataGridControl x:Name="OrderDetailsGrid"
                          ItemsSource="{Binding Source={StaticResource cvs_orderdetails}}"
                          PropertyChanged="CurrentItemChanged"
                          DockPanel.Dock="Bottom"/>
  </DockPanel>
</Grid>

The following code provides the implementation of the PropertyChanged event handler.

VB.NET
Copy Code
Private Sub CurrentItemChanged( ByVal sender As Object, _
                                ByVal e AsPropertyChangedEventArgs  )
  If e.PropertyName = "CurrentItem" Then
    If Me.OrderDetailsGrid.CurrentItem Is Nothing Then
      Return
    End If
    Dim group As CollectionViewGroup =
              Me.OrderDetailsGrid.GetParentGroupFromItem( Me.OrderDetailsGrid.CurrentItem )
    Me.CurrentGroupTitle.DataContext = group
    Me.TotalOrders.DataContext = group
    Me.AveragePrice.DataContext = group
  End If
End Sub
C#
Copy Code
private void CurrentItemChanged( object sender, PropertyChangedEventArgs e )
{
  if( e.PropertyName == "CurrentItem" )
  {
    if( this.OrderDetailsGrid.CurrentItem == null )
      return;
    CollectionViewGroup group =
              this.OrderDetailsGrid.GetParentGroupFromItem( this.OrderDetailsGrid.CurrentItem );
    this.CurrentGroupTitle.DataContext = group;
    this.TotalOrders.DataContext = group;
    this.AveragePrice.DataContext = group;
  }
}